﻿using HalconDotNet;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Net.Mime.MediaTypeNames;

namespace HalconWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        HImage hImage = new HImage();
        HTuple modelId = new HTuple();

        HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
        HTuple hv_Angle = new HTuple(), hv_Score = new HTuple();

        HDrawingObject rect;
        HDrawingObject circle;

        List<HDrawingObjectInfo> DrawList = new List<HDrawingObjectInfo>();

        /// <summary>
        /// 生成矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnRect_Click(object sender, RoutedEventArgs e)
        {
            string color = "blue";
            rect = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1, 100, 200, 300, 400);
            rect.SetDrawingObjectParams("color", color);

            rect.OnDrag(onDragDrawingObject);
            rect.OnResize(onResizeDrawingObject);
            rect.OnSelect(onSelectDrawingObject);

            // 将绘制的对象添加到集合中
            DrawList.Add(new HDrawingObjectInfo()
            {
                Type = rect.GetDrawingObjectParams("type"),
                Color= color,
                HDrawingObject= rect,
                HTuples=rect.GetHTuples(),
            });


            HSmart.HalconWindow.AttachDrawingObjectToWindow(rect);
        }

        /// <summary>
        /// 生成圆形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCircle_Click(object sender, RoutedEventArgs e)
        {
            string color = "red";
            circle = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE, 100, 200, 50);
            circle.SetDrawingObjectParams("color", color);

            circle.OnDrag(onDragDrawingObject);
            circle.OnResize(onResizeDrawingObject);
            circle.OnSelect(onSelectDrawingObject);

            DrawList.Add(new HDrawingObjectInfo()
            {
                Type=circle.GetDrawingObjectParams("type"),
                Color = color,
                HDrawingObject = circle,
                HTuples=circle.GetHTuples(),
            });


            HSmart.HalconWindow.AttachDrawingObjectToWindow(circle);
        }

        /// <summary>
        /// 拖动时触发
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        public void onDragDrawingObject(HDrawingObject drawid, HWindow window, string type)
        {
            refreshDrawingObject(drawid);
        }

        /// <summary>
        /// 大小改变时触发
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        public void onResizeDrawingObject(HDrawingObject drawid, HWindow window, string type)
        {
            refreshDrawingObject(drawid);
        }

        /// <summary>
        /// 选中时触发
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        public void onSelectDrawingObject(HDrawingObject drawid, HWindow window, string type)
        {
            refreshDrawingObject(drawid);
        }

        /// <summary>
        /// 刷新绘制对象内部的结构数组参数
        /// </summary>
        /// <param name="drawid"></param>
        public void refreshDrawingObject(HDrawingObject drawid)
        {
            var drawInfo = DrawList.FirstOrDefault(t => t.HDrawingObject.ID.Equals(drawid.ID));

            if (drawInfo != null)
            {
                drawInfo.HTuples = drawid.GetHTuples();
            }
        }

        /// <summary>
        /// 获取矩形区域图像并创建匹配模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnGenRect_Click(object sender, RoutedEventArgs e)
        {
            var draw = DrawList.FirstOrDefault(t => t.Type.Equals("rectangle1"));
            if (draw != null)
            {
                // 根据绘制对象生成对应区域
                HObject hrectObj = draw.GenHObject();

                // 计算区域
                HObject partImage;
                // 裁剪图像
                HObject cropImage;
                HOperatorSet.ReduceDomain(hImage, hrectObj, out partImage);
                HOperatorSet.CropDomain(partImage, out cropImage);

                // 显示图像
                HSmart.HalconWindow.DispObj(cropImage);

                // 生成模板
                HOperatorSet.CreateShapeModel(cropImage, "auto", (new HTuple(0).TupleRad()), (new HTuple(360).TupleRad()), "auto", "auto",
                "use_polarity", "auto", "auto", out modelId);

                // 下载图片到本地
                HOperatorSet.WriteImage(cropImage, "png", 0, "C:/Users/15507/Desktop/视觉学习/test/crop.png");

                // 下载模板到本地
                HOperatorSet.WriteShapeModel(modelId, "C:/Users/15507/Desktop/视觉学习/test/crop.shm");
            }
        }

        /// <summary>
        /// 绘制圆形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private  void BtnDrawCircle_Click(object sender, RoutedEventArgs e)
        {
            

        }

        /// <summary>
        /// 绘制矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void BtnDrawRect_Click(object sender, RoutedEventArgs e)
        {
            HTuple row1 = new HTuple();
            HTuple column1 = new HTuple();
            HTuple row2 = new HTuple();
            HTuple column2 = new HTuple();

            HObject hGenObject;

            HObject hReduceObject;

            HObject hCropObject;

            await Task.Run(() =>
            {
                HOperatorSet.SetColor(HSmart.HalconWindow, "blue");

                // 绘制矩形
                HOperatorSet.DrawRectangle1(HSmart.HalconWindow, out row1, out column1, out row2, out column2);

                // 生成矩形
                HOperatorSet.GenRectangle1(out hGenObject, row1, column1, row2, column2);

                // 计算区域
                HOperatorSet.ReduceDomain(hImage, hGenObject, out hReduceObject);

                // 切割图片
                HOperatorSet.CropDomain(hReduceObject, out hCropObject);

                // 生成模板
                HOperatorSet.CreateShapeModel(hCropObject, "auto", (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), "auto", "auto", "use_polarity", "auto", "auto", out modelId);

                HOperatorSet.WriteImage(hCropObject, "png", 0, "C:/Users/15507/Desktop/视觉学习/test/crop.png");

                HOperatorSet.WriteShapeModel(modelId, "C:/Users/15507/Desktop/视觉学习/test/crop.shm");
            });
        }

        




        /// <summary>
        /// 读取图片点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnReadImage_Click(object sender, RoutedEventArgs e)
        {
            // 读取读取原版图片
            hImage.ReadImage( "C:/Users/15507/Pictures/5halcon.png");
            // 显示原版图片
            HSmart.HalconWindow.DispObj(hImage);
        }

        /// <summary>
        /// 定位结果点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnRun_Click(object sender, RoutedEventArgs e)
        {
            if (modelId.Length < 1)
            {
                return;
            }

            // 获取匹配模板
            //HOperatorSet.ReadShapeModel("C:/Users/15507/Desktop/halconTestDemo/shape.shm", out modelId);
            // 查找匹配模板
            HOperatorSet.FindShapeModel(hImage, modelId, -0.39, 0.79, 0.5, 1, 0.5,
      "least_squares", 0, 0.9, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);
            // 显示查找的内容
            txtResult.Text = $"分数:{hv_Score.DArr[0]} Row坐标:{hv_Row.DArr[0]} Column坐标:{hv_Column.DArr[0]} 角度:{hv_Angle.DArr[0]}";
            // 设置颜色
            HOperatorSet.SetColor(HSmart.HalconWindow, "red");
            // 绘制十字架标记找到
            HOperatorSet.DispCross(HSmart.HalconWindow, hv_Row.DArr[0], hv_Column.DArr[0], 30, hv_Angle.DArr[0]);
        }
    }
}